<?php

namespace app\admin\controller\system;

use app\admin\controller\Base;
use app\common\model\auth\AuthAdminModel;
use app\common\model\auth\AuthAdminOperateLogModel;
use app\common\model\auth\AuthMenuModel;
use app\common\model\system\SystemAbnormalModel;

class Abnormal extends Base
{
    /**
     * 列表
     */
    public function list()
    {
        $params = request()->only(['page', 'limit', 'field_type', 'field_value', 'role_ids', 'start_date', 'end_date']);

        $sqlObj = (new SystemAbnormalModel)->order('id', 'DESC');
        if (isset($params['role_ids']) && !empty($params['role_ids'])) {
            $adminIds = (new AuthAdminModel)->where("concat(',', role_ids , ',') regexp concat(',',replace('{$params['role_ids']}',',',',|,'),',')")
                ->field('id')
                ->column('id');
            $sqlObj->whereIn('admin_id', $adminIds);
        }
        if (isset($params['field_type'], $params['field_value']) && !empty($params['field_value'])) {
            switch ($params['field_type']) {
                case 'name'://搜理员名称
                    $adminIds = (new AuthAdminModel)->whereLike('name', "%{$params['field_value']}%")
                        ->field('id')
                        ->column('id');
                    $sqlObj->whereIn('admin_id', $adminIds);
                    break;
                case 'mobile'://搜管理手机
                    $adminIds = (new AuthAdminModel)->whereLike('mobile', "%{$params['field_value']}%")
                        ->field('id')
                        ->column('id');
                    $sqlObj->whereIn('admin_id', $adminIds);
                    break;
                case 'menu_name'://搜菜单名称
                    $menuIds = (new AuthMenuModel)->whereLike('name', "%{$params['field_value']}%")
                        ->field('id')
                        ->column('id');
                    $sqlObj->whereIn('menu_id', $menuIds);
                    break;
                case 'menu_name_path'://搜菜单路径
                    $sqlObj->whereLike('menu_name_path', "%{$params['field_value']}%");
                    break;
                case 'address'://搜地址
                    $sqlObj->whereLike('address', "%{$params['field_value']}%");
                    break;
                case 'device'://搜设备
                    $sqlObj->whereLike('device', "%{$params['field_value']}%");
                    break;
                case 'ip'://搜IP
                    $sqlObj->where('ip', $params['field_value']);
                    break;
            }
        }

        if (isset($params['start_date'], $params['end_date']) && $params['start_date'] && $params['end_date']) {
            $sqlObj->whereBetween('create_time', [$params['start_date'], $params['end_date'] . ' 23:59:59']);
        }

        $list = $sqlObj->paginate([
            'list_rows' => $params['limit'] ?? 10,
            'page' => $params['page'] ?? 1
        ]);
        $adminData = (new AuthAdminModel)->column('id,name,mobile,status', 'id');
        foreach ($list->items() as &$v) {
            $v['admin_name'] = $adminData[$v['admin_id']]['name'] ?? '';
            $v['mobile'] = $adminData[$v['admin_id']]['mobile'] ?? '';
            $v['admin_status_attr'] = (new AuthAdminModel)->status[$adminData[$v['admin_id']]['status'] ?? ''] ?? '';
        }

        return response_success('', [
            'count' => $list->total(),
            'data' => $list->items()
        ]);
    }

    /**
     * 删除
     */
    public function del()
    {
        $params = request()->only(['ids']);
        $this->validate($params, [
            'ids' => 'require'
        ], [
            'ids.require' => '请选择删除日志'
        ]);

        (new SystemAbnormalModel)->whereIn('id', explode(',', $params['ids']))->delete();

        return response_success();
    }

}